<?php
declare (strict_types=1);

namespace app\school\logic\school\admin;

use app\school\model\school\admin\SchoolBalanceFlow as BalanceFlowModel;
use app\user\logic\user\Account;

/**
 * 机构记录逻辑层
 */
class SchoolBalanceFlow extends BalanceFlowModel
{

    /**
     * 机构充值/消费-流水记录
     *
     * @param $data
     * @return mixed
     */
    public function getRecordList($data)
    {
        $map[] = ['mhm_id', '=', $data['school_id']];
        //类型
        if (isset($data['type']) && $data['type'] === 'reduce') {
            $map[] = ['flow_type', '=', 0];
        } else {
            $map[] = ['flow_type', '=', 1];
        }
        //时间
        if (isset($data['time_slot'])) {
            $start_time = strtotime($data['time_slot'][0]);
            $end_time   = strtotime($data['time_slot'][1]);
            $map[]      = ['create_time', 'between', [$start_time, $end_time]];
        }

        return $this->where($map)->with(['user_name'])->order('create_time', 'desc')->paginate($this->getListRows())->each(function ($i) {
            // 手动组装内容
            if ($i->rel_type == 'user') {
                $i->user_name = app(Account::class)->whereId($i->rel_pk)->value('user_name');
            }
        });
    }
}
